package com.example.weixinpayment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.weixinpayment.constant.enums.wxpay.WxRefundStatus;
import com.example.weixinpayment.entity.OrderInfo;
import com.example.weixinpayment.entity.RefundInfo;
import com.example.weixinpayment.mapper.RefundInfoMapper;
import com.example.weixinpayment.service.OrderInfoService;
import com.example.weixinpayment.service.RefundInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.weixinpayment.util.OrderNoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Autowired
    private OrderInfoService orderInfoService;

    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {

        //根据订单号获取订单信息
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);

        //根据订单号生成退款订单
        RefundInfo refundInfo = new RefundInfo();
        //订单编号
        refundInfo.setOrderNo(orderNo);
        //退款单编号
        refundInfo.setRefundNo(OrderNoUtils.getRefundNo());
        //原订单金额(分)
        refundInfo.setTotalFee(orderInfo.getTotalFee());
        //退款金额(分)
        refundInfo.setRefund(orderInfo.getTotalFee());
        //退款原因
        refundInfo.setReason(reason);

        //保存退款订单
        baseMapper.insert(refundInfo);

        return refundInfo;
    }

    @Override
    public void updateRefund(String content) {
        //json字符串转map
        HashMap<String, Object> result = JSONObject.parseObject(content, HashMap.class);

        //设置要修改的字段
        RefundInfo refundInfo = new RefundInfo();
        //微信支付退款单号
        refundInfo.setRefundId((String) result.get("refund_id"));

        //查询退款和申请退款中的返回参数
        if(result.get("status") != null){
            //退款状态
            refundInfo.setRefundStatus((String)result.get("status"));
            //将全部响应结果存入数据库的content字段
            refundInfo.setContentReturn(content);
        }
        //退款回调中的回调参数
        if(result.get("refund_status") != null){
            //退款状态
            refundInfo.setRefundStatus((String)result.get("refund_status"));
            //将全部响应结果存入数据库的content字段
            refundInfo.setContentNotify(content);
        }

        baseMapper.update(refundInfo, new QueryWrapper<RefundInfo>().eq("refund_no", result.get("out_refund_no")));
    }

    @Override
    public List<RefundInfo> getNoRefundOrderByDuration(int minutes) {
        //minutes分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));

        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund_status", WxRefundStatus.PROCESSING.getType());
        queryWrapper.le("create_time", instant);
        List<RefundInfo> refundInfoList = baseMapper.selectList(queryWrapper);
        return refundInfoList;
    }
}
